package cn.yuanqiao.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yuanqiao.common.core.domain.BusinessActivity;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.system.domain.TenantOperLog;
import cn.yuanqiao.system.mapper.TenantOperLogMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.yuanqiao.system.domain.SysOperLog;
import cn.yuanqiao.system.mapper.SysOperLogMapper;
import cn.yuanqiao.system.service.ISysOperLogService;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Autowired
    private SysOperLogMapper operLogMapper;

    @Autowired
    private TenantOperLogMapper tenantOperLogMapper;

    private static Map<String,Boolean> logMap = new HashMap<>();

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog)
    {
        String tenantCode = operLog.getTenantCode();
        operLog.setOperId(SnowflakeIdGenerator.generateId());
        if(StringUtils.isNotEmpty(tenantCode)){
            TenantOperLog tenantOperLog = new TenantOperLog(tenantCode);
            tenantOperLog.setOperId(SnowflakeIdGenerator.generateId());
            BeanUtils.copyProperties(operLog,tenantOperLog);
            //验证表是否存在
            if(logMap.get(tenantCode) == null){
                int num = tenantOperLogMapper.isExistTable(tenantOperLog.getTableName());
                if(num == 0){
                    tenantOperLogMapper.createSysOperLog(tenantOperLog.getTableName());
                }
                logMap.put(tenantCode,true);
            }
            if(tenantOperLog.getLogLevel() == null){
                if(tenantOperLog.getBusinessType() == 3){//删除
                    tenantOperLog.setLogLevel(1); //警告
                }else {
                    tenantOperLog.setLogLevel(tenantOperLog.getStatus());
                }
            }
            tenantOperLogMapper.insertOperlog(tenantOperLog);
        }
        return operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        return operLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }
    /**
     * 新增业务行为日志
     *
     * @param a001Businessactivity 操作日志对象
     * @return 结果
     */
    @Override
    public int insertA001Businessactivity(BusinessActivity a001Businessactivity) {
        return operLogMapper.insertA001Businessactivity(a001Businessactivity);
    }

    @Override
    public List<BusinessActivity> selectA001BusinessactivityList(BusinessActivity a001Businessactivity)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        a001Businessactivity.setTableName(tenantCode+"_BUSINESSACTIVITY");
        return operLogMapper.selectA001BusinessactivityList(a001Businessactivity);
    }

    @Override
    public List<Long[]> operLogTongji(SysOperLog sysOperLog) {
        Map<String,Long> map=operLogMapper.operLogTongji(sysOperLog);
        List list=new ArrayList(6);
        for (Map.Entry<String,Long> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }
}
